package com.zsy.oauth2.controller;

import com.zsy.base.bean.DemoResult;
import com.zsy.base.constant.Constants;
import com.zsy.oauth2.service.AuthService;
import com.zsy.common.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @program: my-springcloud
 * @description: 登陆Controller
 * @author: zhaoshouyun
 * @create: 2020-04-17 13:26
 **/
@Api(tags="登录请求")
@Slf4j
@Controller
@RequestMapping("/oauth")
public class LoginController {
    @Autowired
    private AuthService authService;

    @Value("${auth.clientId}")
    private String clientId;

    @Value("${auth.clientSecret}")
    private String clientSecret;

    @Value("${auth.cookieDomain}")
    private String cookieDomain;

    @Value("${auth.cookieMaxAge}")
    private int cookieMaxAge;

    @Autowired
    RedisUtils redisUtils;

    @GetMapping("/toLogin")
    @ApiIgnore
    public String toLogin(){
        log.info("跳转到登陆页面！");
        return "login";
    }

    /**进行登陆
     * @Author zhaoshouyun
     * @Description
     * @Date 2020-04-17 05:36
     * @Param username
     * @Param password
     * @return com.zsy.base.bean.DemoResult
     **/
    @PostMapping("login")
    @GetMapping("login")
    @ResponseBody
    @ApiOperation(value="获取token", notes="根据用户名和密码获取token")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "password", value = "密码", required = true, dataType = "String")
    })
  public DemoResult login(@RequestParam(value="username")String username, @RequestParam(value="password")String password, @ApiIgnore HttpServletResponse response){
        if (StringUtils.isBlank(username)){
            return DemoResult.FAIL("用户名不能为空");
        }
        if (StringUtils.isBlank(password)){
            return DemoResult.FAIL("密码不能为空");
        }
        log.info("进行登陆，用户名：{}",username);
        DemoResult result = authService.login(username, password, clientId, clientSecret);

        if (result.isSuccess()) {
            Map map = (Map) result.getData();
            /*Object jti = map.get("jti");
            if (jti != null) {
                redisUtils.set(Constants.REDIS_TOKEN_KEY+jti.toString(), map);
            }*/
            redisUtils.set(Constants.REDIS_TOKEN_KEY, map);
        }
        log.info("用户名：{}----登陆返回结果：{}",username,result);
        return result;
    }

    @PostMapping("refreshToken")
    @GetMapping("refreshToken")
    @ResponseBody
    @ApiOperation(value="刷新token", notes="根据刷新token获取新token")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "refreshToken", value = "刷新token", required = true, dataType = "String")
    })
    public DemoResult refreshToken(@RequestParam(value="refreshToken")String refreshToken, @ApiIgnore HttpServletResponse response){
        if (StringUtils.isBlank(refreshToken)){
            return DemoResult.FAIL("刷新token不能为空");
        }
        log.info("进行刷新token");
        DemoResult result = authService.refreshToken(refreshToken, clientId, clientSecret);
        return result;
    }
}
